package cn.wl.gulimall.order.config;

import cn.wl.gulimall.order.vo.PayVo;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117615420";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCy+VqyxgV76sTbOMndE6iSxWZokuQ30LNE6D1z0xXCuwLOFcfYMLjMBRDVQh04JY3lCj/6PS6zLmJJToE2IvrUVnVkB99daPC2Hb7ZWhddtyOUOqcMqS+ss/+sztAkEFSyoWoaMmArdMPpX51xUM9NunBBHb3pLIau/NsYRCHH7OUOyLFPkzF8ed09Thslgf49v88h7xNeWtIa0jFSP9EspCy7hohXL7WK9N5wYQHzqCcs3H5M03bT6VRwGoor5lHSIELKDVkjURxm06D78L6rD6AsA+jvWGAngdIhahXEhTNCIDw+SFqEBYE5dhfwFbRaTONFd+7xe94fgBLr/WyxAgMBAAECggEBAIsaZZUXdcG3WeOtQrASjG3vAUZ8DOcYzYzcGI9CW+JrKaX1pcrxK2Be+5NU/JLcFWoUSl97WCWqAnhJjQH/tAZ/YGQr/j6Cxrw1aFVUwV50bjHSWG3tvYE0sZ6la37sNnhUHrxXtQlxiqeDSkJ9dXJLWt4xeJ6XneK54qv8oGja9mi2ku7uxsmnx82Eoe08I7Zf5dIcIdx0sBAc+U+pptM79Qhrtrm8Ju2xZRJYYQiLSun5vnSwHQM95xPyx7CwYPFfeA1Kn6w9LeCYfDYRkAu3B1rvR70/PvFDYuNWwG3qTohQJlswEu6jlwdVQ13epdwarYDrG3QiLyKMAYn1BkECgYEA8mpMoHsdWDCou/YG6vP60pxBGA0zgn4PBnMyf6ue80ZIygFTkQo7GdDSGnu052ZyOASbVsZtMEi8hPMroVLTIdqJVJOHab5+O6boEdbczLmbyH46WXNVH/1iD96eM273vyFe7sIb5A6OxjFIgFAw4ozBa9CIV4FY7igpQQS+NtkCgYEAvQDsr5432gxAp9P9GwFa8EDV3epZpzKoCWhxr3yRSMjQ+nW5PrrzfT8NS918ZZKFdd4s+oWMGzwCQVvS6Z1Smt9eO25KkLpvKkdMelw8xOum//smzRK6GVPXeg5wdKR0RFq9UaExEh/41zElpys0UHHWBYS+ejd7dfNkpopurZkCgYBweA0RL82PixPeEOlovMWUTVaOGCHb2yyRa4T0YuWeLO+8eR0SX9LHyXCRRO/LpGY9wzVm7yzdiAGfFUsaz9iRDllE2iWrU+wxNODa1fsfKMY0IQxlT/MgomiKwFhZuJRNIuhukbRq/K4VGexvS8VCdteOc/Ot1Z8y74MkXVT7EQKBgED7wWU8bA2fateJTpvrYl36nM3ZFowjIZ1ieLwSg01YJmoG8fM8ih1FAYhVFcFCmHqHMkXMhviSMGBSdvj1q4IuTJL4f9C1GSWbMBNeBVLbhya6LoQR7URqDEseDdI8/PDHlhIr7thBf+hVMJICi9Eg4qoxEuColN1gGvob3KP5AoGBAKwCCGa8gqWx6P4ydj4V0gC+e1UKdLzTRnJ10a4iU0yUhzjLFpJQXlBu/cMbSdLBw3nKpBxOuNnLRtNob2PDgMISUF26uHVtXuZY/XaTCETjwdOSBKpBYY8zUfBCW2Ti61xAI23kooJ0oQ59aDovd03cbiXSeTEm9kQfG15er314";    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApTrgUsWTnq8M/LUd6YdtVxfLOh3nG1F1aMEYmck4zpQRrxfBDc1XbW6jx+rfxnzm1KP+mUl6fvibWY80YpyVK6SLepj0UMqzWu0rXPj11l5WmXNHyKlr9IYKsL1pHyDmMS0LRLKe0A9KCAHBSIYq8C17iCQa5P5brdXeJd5ZJwEZfg3kkHiQKAMzq6G5eDGFu4m00mu48LkOMHxoe/5u7fg7TPtLEz+9qmoheqCdVG0s6s1Bno3KflRxVruvc1hndGNQr2lyNxixGqr0CYL+3HYs+U/+Tb9QCzesQJLBxb1vsJqTEI9IG8pmFyk2sV6pR5BxJxhek9QOyYgf6jEy9QIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://xhnvfvm1tt.52http.tech/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";
    private String timeout="30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
